<?php


namespace App\backend\controllers\goods;


use App\backend\controllers\AdminBaseController;
use App\backend\models\GoodsModel;
use App\backend\services\GoodsServices;
use App\Utils\JsonResp;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class GoodsController extends AdminBaseController
{
    protected $class_name = 'Goods';

    protected $goodsServices;

    public function __construct(GoodsServices $goodsServices)
    {
        $this->goodsServices = $goodsServices;
    }

    /**
     * 商品列表
     */
    public function getGoodsList(Request $request)
    {
        $data = $request->all();
        $list = GoodsServices::getGoodsList($data);
        return JsonResp::success('成功', $list);
    }

    /**
     * 添加/编辑 商品 基础信息
     */
    public function modifyBaseGoods(Request $request)
    {
        $data = $request->all();
        $validator = Validator::make(
            $data,
            GoodsModel::rules(),
            GoodsModel::message()
        );
        if ($validator->fails()) {
            $msg = $validator->errors()->all();
            return JsonResp::error($msg[0]);
        }
        $list = GoodsServices::modifyBaseGoods($data);
        return JsonResp::success('成功', $list);
    }

    /**
     * 添加/编辑 商品 价格/库存
     */
    public function modifyPriceStockGoods(Request $request)
    {
        $data = $request->all();
        $validator = Validator::make(
            $data,
            GoodsModel::modify_price_stock_rules(),
            GoodsModel::message()
        );
        if ($validator->fails()) {
            $msg = $validator->errors()->all();
            return JsonResp::error($msg[0]);
        }
        $list = GoodsServices::modifyPriceStockGoods($data);
        return JsonResp::success('成功', $list);
    }

    /**
     * 添加/编辑 商品 附加规格
     */
    public function modifySkuInfoGoods(Request $request)
    {
        $data = $request->all();
        $validator = Validator::make(
            $data,
            GoodsModel::modify_sku_info_rules(),
            GoodsModel::message()
        );
        if ($validator->fails()) {
            $msg = $validator->errors()->all();
            return JsonResp::error($msg[0]);
        }
        $list = GoodsServices::modifySkuInfoGoods($data);
        return JsonResp::success('成功', $list);
    }

    /**
     * 添加/编辑 商品 物流设置
     */
    public function modifyLogisticsGoods(Request $request)
    {
        $data = $request->all();
        $validator = Validator::make(
            $data,
            GoodsModel::modify_logistics_rules(),
            GoodsModel::message()
        );
        if ($validator->fails()) {
            $msg = $validator->errors()->all();
            return JsonResp::error($msg[0]);
        }
        $list = GoodsServices::modifyLogisticsGoods($data);
        return JsonResp::success('成功', $list);
    }

    /**
     * 添加/编辑 商品 详情
     */
    public function modifyDescriptionGoods(Request $request)
    {
        $data = $request->all();
        $validator = Validator::make(
            $data,
            GoodsModel::modify_description_rules(),
            GoodsModel::message()
        );
        if ($validator->fails()) {
            $msg = $validator->errors()->all();
            return JsonResp::error($msg[0]);
        }
        $list = GoodsServices::modifyDescriptionGoods($data);
        return JsonResp::success('成功', $list);
    }

    /**
     * 添加/编辑 商品 参数
     */
    public function modifyParamsGoods(Request $request)
    {
        $data = $request->all();
        $validator = Validator::make(
            $data,
            GoodsModel::modify_params_rules(),
            GoodsModel::message()
        );
        if ($validator->fails()) {
            $msg = $validator->errors()->all();
            return JsonResp::error($msg[0]);
        }
        $list = GoodsServices::modifyDescriptionGoods($data);
        return JsonResp::success('成功', $list);
    }

    /**
     * 添加/编辑 商品 状态
     */
    public function modifyStateGoods(Request $request)
    {
        $data = $request->all();
        $validator = Validator::make(
            $data,
            GoodsModel::modify_state_rules(),
            GoodsModel::message()
        );
        if ($validator->fails()) {
            $msg = $validator->errors()->all();
            return JsonResp::error($msg[0]);
        }
        $list = GoodsServices::modifyStateGoods($data);
        return JsonResp::success($list);
    }

    /**
     * 添加/编辑 商品 限购
     */
    public function modifyQuotaGoods(Request $request)
    {
        $data = $request->all();
        $validator = Validator::make(
            $data,
            GoodsModel::modify_quota_rules(),
            GoodsModel::message()
        );
        if ($validator->fails()) {
            $msg = $validator->errors()->all();
            return JsonResp::error($msg[0]);
        }
        $list = $this->goodsServices->modifyQuotaGoods($data);
        return JsonResp::success($list);
    }

    /**
     * 下架/删除商品
     */
    public function removalDelGoods(Request $request)
    {
        $data = $request->all();
        $validator = Validator::make(
            $data,
            GoodsModel::update_rules(),
            GoodsModel::message()
        );
        if ($validator->fails()) {
            $msg = $validator->errors()->all();
            return JsonResp::error($msg[0]);
        }
        GoodsServices::updateGoodStatus($data);
        return JsonResp::success('成功', []);
    }

    /**
     * 商品详情
     */
    public function getGoodsInfo(Request $request)
    {
        $data = $request->all();
        $validator = Validator::make(
            $data,
            GoodsModel::info_rules(),
            GoodsModel::message()
        );
        if ($validator->fails()) {
            $msg = $validator->errors()->all();
            return JsonResp::error($msg[0]);
        }
        $list = GoodsServices::getGoodsInfo($data);
        return JsonResp::success('成功', $list);
    }

}
